package ai.agents;

import java.awt.Graphics;

import ai.application.Config;
import ai.application.Environment;
import ai.gui.Drawable;
import ai.moving_objects.Moveable;
import ai.spatial.Vector2D;


public class Agent implements Moveable, Drawable {

	private Vector2D desiredVelocity;
	private Vector2D currentVelocity;
	private Vector2D position;
	
	@Override
	public Vector2D getPosition() {
		return position;
	}

	@Override
	public void setPosition(Vector2D position, Environment envvironment) {
		this.position = position;
	}

	@Override
	public void update(Environment environment) {
		move(environment);
	}
	
	protected void move(Environment environment)
	{
		desiredVelocity = new Vector2D(0.3 * desiredVelocity.getX(), 0.3 * desiredVelocity.getY());//a.getDesiredVelocity();
		
		double accelerationX = Config.getSimStepSize()*(currentVelocity.getX() + desiredVelocity.getX())/Config.TAU;
		double accelerationY = Config.getSimStepSize()*(currentVelocity.getY() + desiredVelocity.getY())/Config.TAU;
		double newVelocityX = currentVelocity.getX() + accelerationX;
		double newVelocityY = currentVelocity.getY() + accelerationY;
		
		// restrict velocity to max value
		double scale = environment.validateVelocity(newVelocityX, newVelocityY);
		newVelocityX = scale * newVelocityX;
		newVelocityY = scale * newVelocityY;
		double moveVelocityX = newVelocityX * Config.getSimStepSize();
		double moveVelocityY = newVelocityY * Config.getSimStepSize();
		
		double newPositionX = position.getX() + moveVelocityX;
		double newPositionY = position.getY() + moveVelocityY;
		
		Vector2D newPosition = new Vector2D(newPositionX, newPositionY);
		
		if (!environment.checkCollision(newPosition)) {
			position = newPosition;
			currentVelocity.setX(newVelocityX);
			currentVelocity.setY(newVelocityY);
		} else {
			currentVelocity.setX(0);
			currentVelocity.setY(0);
		}		
	}

	@Override
	public Vector2D getDesiredVelocity() {
		return desiredVelocity;
	}

	@Override
	public Vector2D getCurrentVelocity() {
		return currentVelocity;
	}

	@Override
	public void draw(Graphics g) {
	}
}
